import com.sun.glass.ui.Size;

public class SingleLinkedList {
    static class ListNode {
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;
    //头插
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }
    //展示单链表
    public void display(){
        ListNode cur = this.head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }
    //尾插
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if(cur == null){
            addFirst(data);
        }
        else{
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
        }
    }
    //得到单链表的长度
    public int size(){
        ListNode cur = this.head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    //从index下标插入
    public boolean addIndex(int index,int data){
        if(index < 0 || index > size()){
            throw new IndexNotLegal("下标不合法！");
        }
        if(index == 0){
            addFirst(data);
        }
        else if(index == size()){
            addLast(data);
        }
        else{
            ListNode cur = this.head;
            ListNode node = new ListNode(data);
            while(index - 1 != 0){
                cur = cur.next;
                index--;
            }
            node.next = cur.next;
            cur.next = node;
         /*   Node cur = this.head;
            Node dp = null;
            Node node = new Node(data);
            while(index != 0){
                dp = cur;
                cur = cur.next;
                index--;
            }
            node.next = cur;
            dp.next = node;*/
        }
        return true;
    }
    //查找
    public boolean contains(int key){
        ListNode cur = this.head;
        while(cur != null){
            if(key == cur.val){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现的key的结点
    public void remove(int key){
        ListNode cur = this.head;
        ListNode dp = cur;
        while(cur != null){
            if(cur.val == key){
                if(head.val == key){
                    head = head.next;
                    return;
                }
                else{
                    dp.next = cur.next;
                    cur = dp.next;
                    return;
                }
            }
            dp = cur;
            cur = cur.next;
        }
        System.out.println("找不到该元素");
    }
    /**
     *     删除所有key的元素
     *     注意要最后删头节点！！！否则如果头节点是关键目标，那么，就是一直在删头节点
     */
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        ListNode cur = this.head;
        ListNode dp = cur;
        boolean flag = true;
        while(cur != null) {
            if (cur.val == key) {
                dp.next = cur.next;//其实这里可以不用找前一个结点，直接插入结点，交换值即可
                cur = dp.next;
                flag = false;
            } else {
                dp = cur;
                cur = cur.next;
            }
        }
        if(head.val == key){
            head = head.next;
            flag = false;
        }
        if(flag){
            System.out.println("找不到该元素");
        }
    }
    public void clear(){
        this.head = null;
    }
    //反转链表
    public ListNode reverseList() {
        if(head == null){
            return null;
        }
        if(head.next == null){
            return head;
        }
        ListNode cur = head.next;
        head.next = null;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
    public void display(ListNode head){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }
    //返回中间结点，偶数则返回会第二个中间结点
    public ListNode middleNode() {
        if(head == null){
            return null;
        }
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
    //输出链表中倒数第K个结点
    public ListNode FindKthToTail(int k) {
        if(k <= 0 || head == null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(k-1 != 0){
                fast = fast.next;
                if(fast == null){
                    return null;//k过大
                }
                k--;
            }
        while(fast.next != null){
                fast = fast.next;
                slow = slow.next;
            }
        return slow;
    }
    //以x为基准分为两部分，一部分大于x,一部分小于x
    public ListNode partition(int x) {
        ListNode as = null;
        ListNode ae = null;
        ListNode bs = null;
        ListNode be = null;
        ListNode cur = head;
        while(cur != null){
            //进a段
            if(cur.val < x){
                //第一次进as
                if(as == null){
                    as = cur;
                    ae = cur;
                }
                else{
                    ae.next = cur;
                    ae = cur;
                }
            }
            else{
                if(bs == null){
                    bs = cur;
                    be = cur;
                }
                else{
                    be.next = cur;
                    be = cur;
                }
            }
            cur = cur.next;
        }
        //如果a段为空
        if(as == null){
            return bs;
        }
        //防止死循环，如果b段存在，那么有可能b段最后一个结点的不为空
        if(bs != null){
            be.next = null;
        }
        ae.next = bs;
        return as;
    }
    //先用一个快慢指针，找出中间位置，通过慢指针反转后半段，最后判断回文
    public boolean chkPalindrome() {
        if(head == null){
            return true;
        }
        ListNode fast = head;
        ListNode slow = head;   //快慢指针注意前后顺序，否则会引起对null指针的解引用操作
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }// 1 2  2 1
        //开始反转
        ListNode cur = slow.next;
        while(cur != null){
            ListNode curNext =cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //判断回文
        cur = head;
        while(cur != slow){
            if(cur.val != slow.val){
                return false;
            }
            if(cur.next == slow){
                return true;
            }
            cur = cur.next;
            slow = slow.next;
        }
        return true;
    }
    //判断链表是否有环
    public boolean hasCycle() {
        //通过快慢指针解决，并且快指针必须是慢指针速度的二倍
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                return true;
            }
        }
        return false;
    }
    public ListNode entranceHasCycle() {

        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                break;
            }
        }
        if(fast == null || fast.next == null){
            return null;
        }
        slow = head;
        while(slow != fast){
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }
}
